package com.itany.nmms.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itany.nmms.constant.DictConstant;
import com.itany.nmms.constant.ResponseCodeConstant;
import com.itany.nmms.entity.Product;
import com.itany.nmms.entity.ProductType;
import com.itany.nmms.exception.FileUploadErrorException;
import com.itany.nmms.exception.ProductExistException;
import com.itany.nmms.exception.RequestParamErrorException;
import com.itany.nmms.service.ProductService;
import com.itany.nmms.service.ProductTypeService;
import com.itany.nmms.util.ParameterUtil;
import com.itany.nmms.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * Author:shixiaojun@itany.com
 * Date:2022/8/3 14:05
 * Version:1.0-SNAPSHOT
 * Description:
 */
@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @Autowired
    private ProductTypeService typeService;

    @ModelAttribute("types")
    public List<ProductType> findTypes(){
        return typeService.findEnable();
    }

    @RequestMapping("/findAll")
    public ModelAndView findAll(String pageNo,String pageSize){
        ModelAndView mav = new ModelAndView();

        if(ParameterUtil.isNull(pageNo)){
            pageNo = DictConstant.PRODUCT_DEFAULT_PAGE_NO;
        }
        if(ParameterUtil.isNull(pageSize)){
            pageSize = DictConstant.PRODUCT_DEFAULT_PAGE_SIZE;
        }

        PageHelper.startPage(Integer.parseInt(pageNo),Integer.parseInt(pageSize));
        List<Product> products = productService.findAll();
        PageInfo<Product> productPage = new PageInfo<>(products);

        // 当执行删除功能,一般留在本页
        // 但是可能存在一种情况
        // 本页数据已经全部被删除
        // 此时应该回到上一页
        // 如果是第一页,留在第一页即可
        // 即:当不是第一页,且这一页没有数据时,回到上一页
        if(!"1".equals(pageNo) && productPage.getList().isEmpty()){
            Integer page = Integer.parseInt(pageNo) - 1;
            mav.setViewName("redirect:findAll?pageNo="+page);
        }else{
            mav.addObject("productPage",productPage);
            mav.setViewName("backend/productManager");
        }

        return mav;
    }

    @ResponseBody
    @RequestMapping("/addProduct")
    public ResponseResult addProduct(String name, String price, @RequestParam("file") MultipartFile file, String typeId){
        ResponseResult result = new ResponseResult();

        try {
            productService.addProduct(name,price,file,typeId);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParamErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (ProductExistException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (FileUploadErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }

        return result;
    }

    @ResponseBody
    @RequestMapping("/findById")
    public ResponseResult findById(String id){
        ResponseResult result = new ResponseResult();

        try {
            Product product = productService.findById(id);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
            result.setReturnObject(product);
        } catch (RequestParamErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }

        return result;
    }

    @ResponseBody
    @RequestMapping("/modifyProduct")
    public ResponseResult modifyProduct(String id,String name, String price, @RequestParam(value = "file",required = false) MultipartFile file, String typeId){
        ResponseResult result = new ResponseResult();

        try {
            productService.modifyProduct(id,name,price,file,typeId);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParamErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (ProductExistException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (FileUploadErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }

    @ResponseBody
    @RequestMapping("/removeProduct")
    public ResponseResult removeProduct(String id){
        ResponseResult result = new ResponseResult();

        try {
            productService.removeById(id);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParamErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }

        return result;
    }

}
